package ShopService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllShop() ([]models.Shop, *gorm.DB) {
	var shops []models.Shop
	Mysql := config.Mysql()
	tx := Mysql.Table("shop__shop").Find(&shops)
	return shops, tx
}

func GetShopById(id int) (models.Shop, *gorm.DB) {
	var shop models.Shop
	Mysql := config.Mysql()
	tx := Mysql.Table("shop__shop").First(&shop, id)
	return shop, tx
}

func GetShopsByP(p map[string]any) ([]models.Shop, *gorm.DB) {
	var shops []models.Shop
	Mysql := config.Mysql()
	tx := Mysql.Table("shop__shop")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&shops)
	return shops, tx
}
func GetShopByP(p map[string]any) (models.Shop, *gorm.DB) {
	var shop models.Shop
	Mysql := config.Mysql()
	tx := Mysql.Table("shop__shop")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&shop)
	return shop, tx
}

func CreateShop(shop models.Shop) {
	Mysql := config.Mysql()
	Mysql.Table("shop__shop").Create(shop)
}

func UpdateShop(shop models.Shop) {
	Mysql := config.Mysql()
	Mysql.Table("shop__shop").Save(shop)
}

func DeleteShop(shop models.Shop) {
	Mysql := config.Mysql()
	Mysql.Table("shop__shop").Delete(shop)
}
